﻿#region Using directives

using System;
using System.Collections.Generic;
using System.Text;

using MetaHeuristics.Tsp;
using MetaHeuristics.Collections;
#endregion

namespace MetaHeuristics.Aco.Collections
{

    public sealed class CityDistanceMatrix : SymmetricMatrix<float>
    {
        public CityDistanceMatrix(CityCollection cities)
            :base(cities.Count,0)
        {
            for(int i=0;i<this.RowCount;++i)
                for (int j = i + 1; j < this.ColumnCount; ++j)
                {
                    int dist = (int)PointMath.GeoDist(cities[i].Position, cities[j].Position);
                    this[i, j] = dist;
                }
        }

        public float ComputeTourLength(TourCollection cityIndexList)
        {
            float length = 0;
            for (int i = 1; i < cityIndexList.Count; ++i)
            {
                int previousCity = cityIndexList[i - 1];
                int currentCity = cityIndexList[i];
                length += this[previousCity, currentCity];
            }

            return length;
        }

        public int NearestCity(int i)
        {
            float dist = float.MaxValue;
            int k = -1;
            for (int j = 0; j < this.ColumnCount; ++j)
            {
                if (this[i, j] < dist)
                {
                    dist = this[i, j];
                    k = j;
                }
            }
            return k;
        }
    }
}
